package com.ziq.base.view.drawable

import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.GradientDrawable.LINEAR_GRADIENT
import android.graphics.drawable.GradientDrawable.RECTANGLE
import android.view.View
import androidx.databinding.BindingAdapter

/**
 * 生成GradientDrawable
 * */
fun gradientDrawableBuild(
    block: GradientDrawableBuilder.()->Unit
):GradientDrawable{
    return GradientDrawableBuilder().also(block).build()
}

class GradientDrawableBuilder{
    var shapeType: Int? = RECTANGLE
    var solidColor: Int? = null
    var width: Int? = null
    var height: Int? = null
    var cornerRadius: Float? = null
    var cornerRadii: FloatArray? = null
    var strokeColor: Int? = null
    var strokeWidth: Int? = null
    var strokeDashGap: Float? = null
    var strokeDashWidth: Float? = null
    var gradientType: Int? = LINEAR_GRADIENT // 渐变类型，线性、放射性、扫描性；默认为线性
    var gradientAngle: GradientDrawable.Orientation? = null
    var gradientColors: IntArray? = null
    var gradientRadius: Float? = null
    var gradientCenterX: Float? = 0.5f //设置渐变中心的X坐标，取值区间[0,1]，默认为0.5，即中心位置
    var gradientCenterY: Float? = 0.5f //设置渐变中心的Y坐标，取值区间[0,1]，默认为0.5，即中心位置

    fun build() : GradientDrawable{
        val gradientDrawable = GradientDrawable()
        val builder = this
        with(gradientDrawable){
            builder.shapeType?.let {
                shape = it
            }

            if(builder.width != null && builder.height != null){
                setSize(builder.width!!, builder.height!!)
            }
            builder.solidColor?.let {
                setColor(it)
            }
            //cornerRadius,cornerRadii 后设置的优先级高
            builder.cornerRadius?.let {
                cornerRadius = it
            }
            builder.cornerRadii?.let {
                cornerRadii = it
            }
            //stroke
            if(builder.strokeColor != null && builder.strokeWidth != null){
                setStroke(builder.strokeWidth!!,
                    builder.strokeColor!!,
                    builder.strokeDashWidth?:0f,
                    builder.strokeDashGap?:0f
                )
            }
            //gradient
            builder.gradientType?.let {
                gradientType = it
            }
            builder.gradientAngle?.let {
                orientation = it
            }
            builder.gradientColors?.let {
                colors = it
            }
            builder.gradientRadius?.let {
                gradientRadius = it
            }
            setGradientCenter(builder.gradientCenterX?:0.5f, builder.gradientCenterY?:0.5f)
        }
        return gradientDrawable
    }
}


@BindingAdapter(value = [
    "bg_shape_type",
    "bg_solid_color",
    "bg_radius",
    "bg_radius_left_top",
    "bg_radius_right_top",
    "bg_radius_left_bottom",
    "bg_radius_right_bottom",
    "bg_stroke_color",
    "bg_stroke_width",
    "bg_stroke_dash_gap",
    "bg_stroke_dash_width",
    "bg_gradient_type",
    "bg_gradient_angle",
    "bg_gradient_colors",
    "bg_gradient_radius",
    "bg_gradient_center_x",
    "bg_gradient_center_y",
    ],
    requireAll = false)
fun setBackground(
    view: View,
    bgShapeType: Int?,
    bgSolidColor: Int?,
    bgRadius: Float?,
    bgRadiusLeftTop: Float?,
    bgRadiusRightTop: Float?,
    bgRadiusLeftBottom: Float?,
    bgRadiusRightBottom: Float?,
    bgStrokeColor: Int?,
    bgStrokeWidth: Float?,
    bgStrokeDashGap: Float?,
    bgStrokeDashWidth: Float?,
    bgGradientType: Int?,
    bgGradientAngle: GradientDrawable.Orientation?,
    bgGradientColors: IntArray?,
    bgGradientRadius: Float?,
    bgGradientCenterX: Float?,
    bgGradientCenterY: Float?,
){
    view.background = gradientDrawableBuild {
        shapeType = bgShapeType?:RECTANGLE
        solidColor = bgSolidColor
        cornerRadius = bgRadius
        if(bgRadiusLeftTop != null
            || bgRadiusRightTop != null
            || bgRadiusLeftBottom != null
            || bgRadiusRightBottom != null){
            cornerRadii = floatArrayOf(bgRadiusLeftTop?:0f, bgRadiusLeftTop?:0f,
                bgRadiusRightTop?:0f, bgRadiusRightTop?:0f,
                bgRadiusRightBottom?:0f, bgRadiusRightBottom?:0f,
                bgRadiusLeftBottom?:0f, bgRadiusLeftBottom?:0f
            )
        }
        strokeColor = bgStrokeColor
        strokeWidth = bgStrokeWidth?.toInt()
        strokeDashGap = bgStrokeDashGap
        strokeDashWidth = bgStrokeDashWidth

        gradientType = bgGradientType
        gradientAngle = bgGradientAngle
        gradientColors = bgGradientColors
        gradientRadius = bgGradientRadius
        gradientCenterX = bgGradientCenterX?:0.5f
        gradientCenterY = bgGradientCenterY?:0.5f
    }
}


class Converter{
    companion object{
        @JvmStatic
        fun convertColorsToIntArray(vararg colors:Int) : IntArray{
            return intArrayOf(*colors)
        }
    }
}











